/**
 * Copyright 2014-2015 www.fsfkj.com
 * All rights reserved.
 * 
 * @project
 * @author jipengfei
 * @version 1.0
 * @date 2015-11-23
 */
package com.eyaomall.wxmanage.common.helper;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.eyaomall.wxmanage.model.pojo.ResOss;
import com.github.pagehelper.StringUtil;
import com.lycheeframework.core.cmp.api.http.GsnData;
import com.lycheeframework.core.cmp.api.http.GsnResponse;

public class OssHelper {
/**
 * 阿里云oss存储帮助类
 */
public static boolean IS_OSS = true;
	
	public static String ACCESS_KEY_ID = "uXJD6OPYLEeYlvmz";
	
	public static String ACCESS_SECRET = "8HWcBD7RRVonXFUBaEvVNHRAnXf9nO";
	
	public static String END_POINT = "http://oss-cn-hangzhou.aliyuncs.com";
	
	public static String OSS_PATH = "http://wmall.oss-cn-hangzhou.aliyuncs.com/";
	
	public static String OSS_IMG_PATH = "http://wmall.img-cn-hangzhou.aliyuncs.com/";
	
	public static String BUCKET_NAME = "wmall";
	
	/**
	 * 上传图片
	 * @param key
	 * @param file
	 * @return
	 */
	public static GsnData upload(String key,MultipartFile file){
		InputStream in = null;
		try{
			OSSClient client = new OSSClient(END_POINT,ACCESS_KEY_ID,ACCESS_SECRET);
			ObjectMetadata meta = new ObjectMetadata();
			// 必须设置ContentLength
			meta.setContentLength(file.getSize());
			meta.setContentType(file.getContentType());
			
			if(StringUtil.isEmpty(key)){
				key = "uploadImg";
			}
			
			Date date = new Date();
			
			String filePath = key + "/" + String.valueOf(date.getTime());
			if(StringUtil.isNotEmpty(file.getOriginalFilename())){
				filePath = filePath + "." + FilenameUtils.getExtension(file.getOriginalFilename());
			}
			
			// 上传Object.
			in = file.getInputStream();
			client.putObject(BUCKET_NAME, filePath,	in, meta);
			
			ResOss ro =  new ResOss();
			ro.setKey(key);
			ro.setFilePath(filePath);
			
			return GsnResponse.toData(ro);
		}catch(Exception e){
			return null;
		}finally{
			if(in != null){
				try {
					in.close();
				} catch (IOException e) {
				}
			}
		}
	}
	
	/**
	 * 删除obj
	 * @param key
	 * @param path
	 * @return
	 */
	public static boolean delObject(String path){
		try{
			OSSClient client = new OSSClient(END_POINT,ACCESS_KEY_ID,ACCESS_SECRET);
			client.deleteObject(BUCKET_NAME, path);
		}catch(Exception e){
			return false;
		}
		
		return true;
	}
	
	/**
	 * 上传文件
	 * @param key
	 * @param file
	 * @return
	 */
	public static String upload(String key,File file){
		FileInputStream in = null;
		try{
			OSSClient client = new OSSClient(END_POINT,ACCESS_KEY_ID,ACCESS_SECRET);
			ObjectMetadata meta = new ObjectMetadata();
			
			// 上传Object.
			in = new FileInputStream(file);
						
			// 必须设置ContentLength
			meta.setContentLength(in.getChannel().size());
			
			if(StringUtil.isEmpty(key)){
				key = "uploadImg";
			}
			
			String filePath = key + "/" + file.getName();
			
			client.putObject(BUCKET_NAME, filePath,	in, meta);
			
			ResOss ro =  new ResOss();
			ro.setKey(key);
			ro.setFilePath(filePath);
			
			return filePath;
		}catch(Exception e){
			return null;
		}finally{
			if(in != null){
				try {
					in.close();
				} catch (IOException e) {
				}
			}
		}
	}
	
	public static void main(String[] args) {
		upload("mall_contract_pdf", new File("/Users/pro/Downloads/pdf/146068521054421.pdf"));
	}
}
